package com.ruoyi.security.elevator.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;

public class URLClientUtil {

	public static String doPostJsonData(String strURL, TreeMap<String, Object> param)
	{
		 String postData =  JSON.toJSONString(param);
		 System.out.println("Post过去的参数：" + postData);
		 String result = doPostJsonData(strURL,postData);
		 return result;
	}

	/**
     * 发送HttpPost请求
     *
     * @param strURL
     *            服务地址
     * @param postData  json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号
     * @return 成功:返回json字符串
     */
	private static String doPostJsonData(String strURL, String postJsonData)
	{
		HttpURLConnection connection = null;
		DataOutputStream out = null;
		BufferedReader reader = null;

		try {
			URL postUrl = new URL(strURL);

			 // 打开连接
	        connection = (HttpURLConnection) postUrl .openConnection();

	        // 设置是否向connection输出，因为这个是post请求，参数要放在 http正文内，因此需要设为true
	        connection.setDoOutput(true);
	        connection.setDoInput(true);
	        connection.setRequestMethod("POST");
	        connection.setInstanceFollowRedirects(true);

	        // Post 请求不能使用缓存
	        connection.setUseCaches(false);

	        connection.setRequestProperty("Content-Type", "application/json");
	        connection.connect();

	        out = new DataOutputStream(connection.getOutputStream());

	        String content = postJsonData;
	        out.writeBytes(content);
	        out.flush();
	        //out.close(); // flush and close

	        reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//设置编码,否则中文乱码
	        String line="";
	        StringBuilder sb = new StringBuilder();

	        while ((line = reader.readLine()) != null){
	           // System.out.println(line);
	        	sb.append(line);
	        }
	        //reader.close();

	        //connection.disconnect();

			return sb.toString();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		finally
		{
			if (out != null) {
				try {
					out.close();
				} catch (Exception ex1) {

				}
			}

			if (reader != null) {
				try {
					reader.close();
				} catch (Exception ex2) {

				}
			}

			if (connection != null) {
				try {
					connection.disconnect();
				} catch (Exception ex3) {

				}
			}
		}
		return "error";
	}

}
